/**
 * 任务管理接口
 *
 * */

import { 
    searchZbJob, 
    editZbJob, 
    deleteZbJob, 
    addZbJob, 
    getlistUserJob, 
    addUserJob, 
    deleteUserJob,
    searchListUserJobReport,
    editUserJobReport,
    deleteUserJobReport,
    addUserJobReport
} from '@/services/jobManage';

export default {
    namespace: 'jobManage',

    state: {
        zbJobUser: [],
        fakeCaptcha: {
            content: []
        },
        listUserJobReport:{
            content: []
        }
    },

    effects: {
        * searchZbJobUser({ payload }, { call, put, select }) {
            const response = yield call(searchZbJob, payload);
            if (!response) return
            yield put({
                type: 'saveState',
                payload: { zbJobUser: response.data },
            });
        },

        * addZbJob({ payload }, { call, put, select }) {
            const response = yield call(addZbJob, payload);
            if (!response) return
        },

        * editZbJob({ payload }, { call, put, select }) {
            const { zbJobUser } = yield select(state => state.jobManage);
            const response = yield call(editZbJob, payload);
            if (!response) return
            yield put({
                type: 'saveState',
                payload: { zbJobUser },
            });
        },

        * deleteZbJob({ payload }, { call, put, select }) {
            const { jobId, index } = payload
            const { zbJobUser } = yield select(state => state.jobManage);
            const response = yield call(deleteZbJob, { jobId });
            if (!response) return
            const newZbJobUser = {...zbJobUser }
            newZbJobUser.content.splice(index, 1)
            newZbJobUser.totalElements-=1
            yield put({
                type: 'saveState',
                payload: { zbJobUser: newZbJobUser },
            });
        },

        //承揽方
        * getlistUserJob({ payload }, { call, put, select }) {
            const response = yield call(getlistUserJob, payload);
            if (!response) return
            yield put({
                type: 'saveState',
                payload: { fakeCaptcha: response.data },
            });
        },

        * addUserJob({ payload }, { call, put, select }) {
            const { fakeCaptcha } = yield select(state => state.jobManage);
            const response = yield call(addUserJob, payload);
            if (!response) return
        },

        * deleteUserJob({ payload }, { call, put, select }) {
            const { userJobId, index } = payload
            const { fakeCaptcha } = yield select(state => state.jobManage);
            const response = yield call(deleteUserJob, { userJobId });
            if (!response) return
            const newFakeCaptcha = {...fakeCaptcha }
            newFakeCaptcha.content.splice(index, 1)
            newFakeCaptcha.totalElements-=1
            yield put({
                type: 'saveState',
                payload: { fakeCaptcha: newFakeCaptcha },
            });
        },

        //进度报告
        * searchListUserJobReport({ payload }, { call, put, select }) {
            const response = yield call(searchListUserJobReport, payload);
            if (!response) return
            yield put({
                type: 'saveState',
                payload: { listUserJobReport: response.data },
            });
        },

        * addUserJobReport({ payload }, { call, put, select }) {
            const response = yield call(addUserJobReport, payload);
            if (!response) return
        },

        * editUserJobReport({ payload }, { call, put, select }) {
            const { listUserJobReport } = yield select(state => state.jobManage);
            const response = yield call(editUserJobReport, payload);
            if (!response) return
            yield put({
                type: 'saveState',
                payload: { listUserJobReport },
            });
        },

        * deleteUserJobReport({ payload }, { call, put, select }) {
            const { listUserJobReport } = yield select(state => state.jobManage);
            const response = yield call(deleteUserJobReport, payload);
            if (!response) return
            const newListUserJobReport = {...listUserJobReport }
            listUserJobReport.content.map((item,index)=>{
                if(item.id == payload.userJobReportId){
                    newListUserJobReport.content.splice(index, 1)
                    newListUserJobReport.totalElements-=1
                }
            })
            yield put({
                type: 'saveState',
                payload: { listUserJobReport: newListUserJobReport },
            });
        },
    },

    reducers: {
        saveState(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};